package com.utilcode.util;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * Created by meijun on 17-5-5.
 */

public class Router {
    private static final String TAG = "Router";
    private Context mContext;
    private final Intent intent;

    public Router(Context context) {
        this.mContext = context;
        intent = new Intent();
    }

    public static Router from(Context context) {

        return new Router(context);
    }

    private Class clazz;

    public Router setAction(Class clazz) {
        this.clazz = clazz;
        return this;
    }

    public void navigation() {
        intent.setClass(mContext, clazz);
        PackageManager packageManager = mContext.getPackageManager();

        try {

            mContext.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            Log.e(TAG, "navigation: 未找到=====》" + clazz.getName());
        }
    }

    public void navigationForResult(int requestCode) {
        intent.setClass(mContext, clazz);
        if (mContext instanceof Activity) {
            try {

                ((Activity) mContext).startActivityForResult(intent, requestCode);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
                Log.e(TAG, "navigationForResult:未找到===》" + clazz.getName());
            }
        }

    }

    public Router setString(String key, String value) {

        intent.putExtra(key, value);

        return this;
    }

    public Router setInt(String key, int value) {

        intent.putExtra(key, value);

        return this;
    }
}
